package com.ruoyi.stock.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock.domain.StockInventory;
import com.ruoyi.stock.service.IStockAlertService;
import com.ruoyi.stock.service.IStockInventoryService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.domain.SysNotice;

/**
 * 库存预警服务实现类
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Service
public class StockAlertServiceImpl implements IStockAlertService 
{
    private static final Logger logger = LoggerFactory.getLogger(StockAlertServiceImpl.class);

    @Autowired
    private IStockInventoryService stockInventoryService;
    
    @Autowired(required = false)
    private ISysNoticeService sysNoticeService;

    /**
     * 检查库存是否低于阈值
     * 
     * @param inventory 库存信息
     * @return 是否需要预警
     */
    @Override
    public boolean checkLowStockAlert(StockInventory inventory)
    {
        if (inventory == null || inventory.getLowerLimit() == null || inventory.getCurrentQuantity() == null) {
            return false;
        }
        
        // 如果当前库存低于下限阈值，需要预警
        return inventory.getCurrentQuantity() <= inventory.getLowerLimit();
    }
    
    /**
     * 发送库存预警通知
     * 
     * @param inventory 库存信息
     */
    @Override
    public void sendLowStockAlert(StockInventory inventory)
    {
        if (inventory == null || sysNoticeService == null) {
            logger.warn("Cannot send stock alert, inventory is null or notice service is not available");
            return;
        }
        
        try {
            // 创建系统通知
            SysNotice notice = new SysNotice();
            notice.setNoticeTitle("库存预警通知");
            notice.setNoticeType("1"); // 通知类型
            notice.setStatus("0"); // 状态正常
            
            StringBuilder content = new StringBuilder();
            content.append("商品【").append(inventory.getProductName() != null ? inventory.getProductName() : "ID:" + inventory.getProductId()).append("】");
            content.append("库存低于警戒线。当前库存：").append(inventory.getCurrentQuantity());
            content.append("，警戒库存：").append(inventory.getLowerLimit());
            content.append("，请及时补充库存！");
            
            notice.setNoticeContent(content.toString());
            notice.setCreateBy("system");
            notice.setCreateTime(DateUtils.getNowDate());
            
            // 保存通知
            sysNoticeService.insertNotice(notice);
            
            logger.info("Stock alert notification sent for product ID: {}", inventory.getProductId());
        } catch (Exception e) {
            logger.error("Failed to send stock alert notification", e);
        }
    }
    
    /**
     * 查询所有库存预警商品
     * 
     * @return 预警商品列表
     */
    @Override
    public List<StockInventory> selectLowStockList()
    {
        List<StockInventory> result = new ArrayList<>();
        
        // 查询所有库存记录
        List<StockInventory> allInventory = stockInventoryService.selectStockInventoryList(new StockInventory());
        
        // 过滤出库存低于阈值的记录
        for (StockInventory inventory : allInventory) {
            if (checkLowStockAlert(inventory)) {
                result.add(inventory);
            }
        }
        
        return result;
    }
} 